package com.cap.dreamcircle.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Bean.MainNewsEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.ViewHolderUtils;
import com.cap.dreamcircle.View.ArticalWebviewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liucxu on 2017/6/21.
 */

public class AdapterMainNews extends BaseAdapter {

    private List<MainNewsEntity> datas = new ArrayList<>();
    private Context mCXontext;

    public AdapterMainNews(List<MainNewsEntity> sources, Context context) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            mCXontext = context;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convert_view, ViewGroup viewGroup) {
        if (convert_view == null) {
            convert_view = View.inflate(mCXontext, R.layout.adapter_main_news,null);
        }
        initViews(convert_view,position);
        return convert_view;
    }

    private void initViews(View convert_view,int position){
        ImageView imv_news_image= ViewHolderUtils.get(convert_view,R.id.imv_news_image);
        TextView tv_news_title=ViewHolderUtils.get(convert_view,R.id.tv_news_title);
        TextView tv_news_desc=ViewHolderUtils.get(convert_view,R.id.tv_news_desc);
        TextView tv_news_like_num=ViewHolderUtils.get(convert_view,R.id.tv_news_like_num);
        TextView tv_news_comment_num=ViewHolderUtils.get(convert_view,R.id.tv_news_comment_num);
        final MainNewsEntity entity=datas.get(position);
        tv_news_like_num.setText(entity.getLikeCount()+"");
        tv_news_comment_num.setText(entity.getCommentCount()+"");
        GlideUtils.getInstance().loadSimpleContextVitMap(mCXontext, Constants.BASE_URL+entity.getImageUrl(),imv_news_image);
        tv_news_title.setText(entity.getTitle());
        tv_news_desc.setText(entity.getSummary());
        convert_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("c449fa35fff24b058a4d5d5e00582d6b");
                Intent intent=new Intent(mCXontext, ArticalWebviewActivity.class);
                intent.putExtra("webview_title",entity.getTitle());
                intent.putExtra("webview_content",entity.getContent());
                intent.putExtra("AdId",entity.getId());
                intent.putExtra("ad_likes",entity.hasLiked());
                intent.putExtra("ad_comment_num",entity.getCommentCount());
                intent.putExtra("ad_like_num",entity.getLikeCount());
                mCXontext.startActivity(intent);
            }
        });
    }

    public void setDatas(List<MainNewsEntity> sources){
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }
}
